package com.app.controller.backstage;

import com.app.mapper.jiuquan.TbcConfigMapper;
import com.app.model.AdminUserModel;
import com.app.model.TbcConfigModel;
import com.app.service.AdminUserService;
import com.app.service.TbcConfigService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ConfigVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.mgobe.v20201014.models.Player;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;



@RestController
@Controller
public class ConfigController {

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    TbcConfigService tbcConfigService;

    @Autowired
    private TbcConfigMapper tbcConfigMapper;

    @GetMapping("/getNewsTypeConfigList")
    public Result<Object> getNewsTypeConfigList(
            int page, int limit,
            @RequestParam(required = false) String keyWord,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Page<TbcConfigModel> configModelPage = tbcConfigService.findByNewsTypePage(keyWord, page, limit);
        ConfigVo configVo = new ConfigVo();
        configVo.setList(configModelPage.getRecords());
        configVo.setCount((int) configModelPage.getTotal());
        return Result.success(configVo);
    }

    @GetMapping("/newsTypeName")
    public String getNewsName(){
        JSONObject json = new JSONObject();
        List<TbcConfigModel> configModelList = findChildren(tbcConfigService.getGroupListByType(6,null),0);
        json.put("data",configModelList);
        json.put("code",200);
        json.put("message","成功");
        return json.toString();
    }

    private List<TbcConfigModel> findChildren(List<TbcConfigModel> modelList, Integer pid){//递归方法
        List<TbcConfigModel> personModelList= new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if(Objects.equals(modelList.get(i).getFatherId(), pid))
            {
                TbcConfigModel model = modelList.get(i);
                List<TbcConfigModel> modelList1 = findChildren(modelList,model.getId());
                for (int j = 0; j < modelList1.size(); j++) {
                    if(modelList1!=null&&modelList1.size()>0)
                    {
                        model.setChild(modelList1);
                    }
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }


    @GetMapping("/getNewsTypeFatherList")
    public Result<Object> getNewsTypeConfigList(HttpServletRequest request) {
        List<TbcConfigModel> configModelList = tbcConfigService.getGroupListByType(6,0);
        return Result.success(configModelList);
    }

    @PostMapping("/editNewsTypeConfig")
    @ResponseBody
    public Result<Object> editNewsTypeConfig(HttpServletRequest request,TbcConfigModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        model.setConType(6);
        tbcConfigMapper.insert(model);
        return Result.success("操作成功");
    }
    @GetMapping("/delNewsTypeConfig")
    public Result<Object> delNewsTypeConfig(Integer id) {
        tbcConfigMapper.deleteById(id);
        return Result.success("操作成功");
    }





}
